package com.shengwei.truck.driver.utils.glide.glide_load_progress;

import com.shengwei.truck.driver.utils.MyLogCat;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class ProgressInterceptor implements Interceptor {
    public static final Map<String, ProgressListener> LISTENER_MAP = new HashMap<>();
 
    public static void addListener(String url, ProgressListener listener) {
        LISTENER_MAP.put(url, listener);
    }
 
    public static void removeListener(String url) {
        LISTENER_MAP.remove(url);
    }
 
    @Override
    public Response intercept(Chain chain) throws IOException {
        /*gxw+s*/
        Request.Builder requestBuilder = chain.request().newBuilder();
        requestBuilder.addHeader("Accept-Encoding", "identity");
        Request request = requestBuilder.build();
        /*gxw+e*/
        Response response = chain.proceed(request);
        String url = request.url().toString();
        MyLogCat.i(" ProgressInterceptor","contentLength progress url="+url);
        ResponseBody body = response.body();
        Response newResponse = response.newBuilder().body(new ProgressResponseBody(url, body)).build();
        return newResponse;
    }
}
